/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.mleiria.generator;

import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.AlgorithmParameterSpec;

/**
 *
 * @author NB13712
 */
public class KeyPairGen {

    private KeyPair pair;
    /**
     * 
     * @param algorithm
     * @param provider
     * @param strength
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException 
     */
    public KeyPairGen(final String algorithm, final String provider, final int strength) 
            throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyPairGenerator kpg;
        if(null == provider){
            kpg = KeyPairGenerator.getInstance(algorithm);
        }else{
            kpg = KeyPairGenerator.getInstance(algorithm, provider);
        }
        kpg.initialize(strength);
        pair = kpg.genKeyPair();
    }
    /**
     * 
     * @param algorithm
     * @param spec 
     * @throws java.security.NoSuchAlgorithmException 
     * @throws java.security.InvalidAlgorithmParameterException 
     */
    public KeyPairGen(final String algorithm, final AlgorithmParameterSpec spec) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {

        final KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
        kpg.initialize(spec);
        pair = kpg.genKeyPair();
    }
    public KeyPairGen(final String algorithm, final int strength) 
            throws NoSuchAlgorithmException, NoSuchProviderException {
        
        this(algorithm, null, strength);
    }
    /**
     * 
     * @return 
     */
    public PublicKey getPublicKey(){
        return pair.getPublic();
    }
    /**
     * 
     * @return 
     */
    public PrivateKey getPrivateKey(){
        return pair.getPrivate();
    }
    /**
     * 
     * @return 
     */
    public KeyPair getKeyPair(){
        return pair;
    }
    
}
